/*
* Amenities Module Edit JS
*
* JSLint.com Check:
*/


// functions


RM.Pages.Functions.Amenities_edit = function(){
    if (RM.Pages.Amenities_List_Columns_SM.getSelections().length === 0) {
        Ext.MessageBox.alert(RM.Translate.Common.Error, RM.Translate.Common.NeedToSelectRows);
        return;
    }
    RM.Pages.Amenities_Edit_MainPanel.show();
};

RM.Pages.Functions.Amenities_add = function(){
    RM.Pages.Amenities_Edit_MainPanel.show();
};

RM.Pages.Functions.Amenities_delete = function(){
    if (RM.Pages.Amenities_List_Columns_SM.getSelections().length === 0) {
        Ext.MessageBox.alert(RM.Translate.Common.Error, RM.Translate.Common.NeedToSelectRows);
        return;
    }
    Ext.MessageBox.confirm(RM.Translate.Common.Delete, RM.Translate.Admin.Amenities.Edit.DeleteAlert, function(buttonID){
        if (buttonID !== 'yes') {
            return;
        }

        var selections = RM.Pages.Amenities_List_Columns_SM.getSelections();
        var parametersJson = [];
        var i = 0;for (i; i < selections.length; i++){
            parametersJson[i] = {
                name : 'ids[]',
                value : selections[i].data.id
            };
        }

        var myMask = new Ext.LoadMask('rm_pages_amenities_mainpage', {msg:RM.Translate.Common.PleaseWait});
        myMask.show();
        var conn = new Ext.data.Connection();
        var request = {
            url: RM.Common.AssembleURL({
                controller : 'Amenities',
                action: 'amenitiesdeletejson',
                parameters : parametersJson
            }),
            method: 'POST',
            success: function(responseObject) {
                RM.Pages.Amenities_List_Json_Store.load();
                myMask.hide();
            },
            failure: function() {
                Ext.MessageBox.alert(RM.Translate.Common.UnconfirmFailure);
                myMask.hide();
            }
        };
        conn.request(request);
    });
};


// toolbar
RM.Pages.Functions.Amenities_Toolbar = {
    xtype : "panel",
    id : "rm_pages_amenities_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([
        {image: RM.BaseLargeImageURL+"delete.gif", label: RM.Translate.Common.Delete, link: "RM.Pages.Functions.Amenities_delete()"},
        {image: RM.BaseLargeImageURL+"edit.gif", label: RM.Translate.Common.Edit, link: "RM.Pages.Functions.Amenities_edit()"},
        {image: RM.BaseLargeImageURL+"new.gif", label: RM.Translate.Common.Add, link: "RM.Pages.Functions.Amenities_add()"}
    ])
};
RM.Toolbars.push(RM.Pages.Functions.Amenities_Toolbar);

// main load function
RM.Pages.Functions.Amenities_ListJson = function(){
    Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_amenities_mainpage');
    RM.Pages.Amenities_List_Columns_SM.clearSelections();
    Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_amenities_toolbar');

    Ext.ux.menu.RangeMenu.prototype.icons = {
        gt: 'img/greater_then.png',
        lt: 'img/less_then.png',
        eq: 'img/equals.png'
    };
    Ext.ux.grid.filter.StringFilter.prototype.icon = 'img/find.png';
    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

    RM.Pages.Amenities_List_Grid.render();
    RM.Pages.Amenities_List_Json_Store.load({params:{start: 0, limit: 300}});

    RM.Help.Load('Admin.Amenities.Main');
};

RM.Pages.Amenities_List_Json_Store_Fields = [
    {name: "id"},
    {name: "name"},
    {name: "units"},
    {name: "icon_url"},
    {name: "info_url"}
];

RM.Pages.Amenities_List_Json_Store = new Ext.ux.grid.livegrid.Store({
    url: RM.Common.AssembleURL({
        controller: 'Amenities',
        action: 'amenitieslistjson'
    }),
    reader: new Ext.ux.grid.livegrid.JsonReader({
        totalProperty: 'total',
        root: 'data'
    },
    RM.Pages.Amenities_List_Json_Store_Fields
    ),
    id: 'rm_amenities_list_grid_json_store',
    sortInfo: {field: 'id', direction: 'DESC'},
    bufferSize : 300
});

RM.Pages.Amenities_List_Columns_SM = new Ext.rm.grid.CheckboxSelectionModel({
    singleSelect: true,
    header: '',
    checkOnly: true
});
RM.Pages.Amenities_List_Columns_Rows = [
    RM.Pages.Amenities_List_Columns_SM,
    {dataIndex: 'id', header: RM.Translate.Common.Id, hidden: false},
    {dataIndex: 'name', header: RM.Translate.Admin.Amenities.Edit.Name, width: 300},
    {dataIndex: 'units', header: RM.Translate.Admin.Amenities.Edit.Units,renderer: function(value, metaData, record, rowIndex, colIndex, store){if (value=="0"){return RM.Translate.Common.AllUnits;} else {return value;}}},
    {dataIndex: 'icon_url', 
        header: RM.Translate.Admin.Amenities.Edit.Icon,
        renderer: function(value, metaData, record, rowIndex, colIndex, store){
            if (value!==""){
                return "<img src='" + RM.UserRootURL + "RM/userdata/images/Amenities/" + value + "' height='30'>";
            }
        }
    },
    {dataIndex: 'info_url', header: RM.Translate.Admin.Amenities.Edit.Link,
        renderer: function(value, metaData, record, rowIndex, colIndex, store){
            if (value!==""){
                return "<a href='" + value + "' target='_blank'>" + value + "</a>";
            }
        }
    }
];

RM.Pages.Amenities_List_Columns = new Ext.grid.ColumnModel({
    columns: RM.Pages.Amenities_List_Columns_Rows,
    defaults: {
        sortable: true
    }
});
RM.Pages.Amenities_List_Filters = new Ext.ux.grid.GridFilters({
    filters: [
        {dataIndex: 'id', type: 'numeric'},
        {dataIndex: 'name', type: 'string'},
        {dataIndex: 'units', type: 'string'},
        {dataIndex: 'icon_url', type: 'string'},
        {dataIndex: 'info_url', type: 'string'}
    ]
});

RM.Pages.Amenities_List_Grid_View = new Ext.ux.grid.livegrid.GridView({
    nearLimit : 100,
    loadMask  : {
        msg :  'Buffering. Please wait...'
    }
});

RM.Pages.Amenities_List_Grid = new Ext.ux.grid.livegrid.GridPanel({
    id : 'rm_amenities_list_grid',
    plugins: RM.Pages.Amenities_List_Filters,
    bbar     : new Ext.ux.grid.livegrid.Toolbar({
            view        : RM.Pages.Amenities_List_Grid_View,
            displayInfo : true
    }),
    enableColLock : false,
    loadMask : {
        msg: RM.Translate.Common.PleaseWait
    },
    height : RM.Common.GetPanelHeight(105),
    cm : RM.Pages.Amenities_List_Columns,
    store : RM.Pages.Amenities_List_Json_Store,
    selModel : RM.Pages.Amenities_List_Columns_SM,
    view: RM.Pages.Amenities_List_Grid_View,
    viewConfig: {
        forceFit: true
    },
    autoExpandColumn: 3
});

// main function
RM.Pages.Amenities_MainPanel = new Ext.Panel({
    id : 'rm_pages_amenities_mainpage',
    title : RM.Translate.Admin.Amenities.Edit.List,
    iconCls: "RM_amenities_root_icon",
    items : [
        RM.Pages.Amenities_List_Grid
    ]
});

RM.Main.Pages.push(RM.Pages.Amenities_MainPanel);